package com.spzx.product.controller;

import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.annotation.RequiresRoles;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author lynn
 * @since 2025-03-12
 */
@RestController
@RequestMapping("/brand")
@Tag(name="品牌管理")
public class BrandController extends BaseController {
    @Autowired
    private  BrandService brandService;


    /**
     * 查询品牌列表
     * @param name
     * @return
     */
    //@RequiresRoles("admin")
    @RequiresPermissions("product:brand:list")
    @GetMapping("/list")
    @Operation(summary = "查询品牌列表",description = "根据品牌名称查询品牌列表")
    public TableDataInfo list(@RequestParam(required = false) String name){
        startPage(); //分页
        List<Brand> list;
        if(StringUtils.hasText(name)){
             list  = brandService.selectBrandListByName(name);
        }else {
            list = brandService.list();
        }
        return getDataTable(list);
    }


    /**
     * 删除品牌
     */
    @RequiresPermissions("product:brand:remove")
    @Log(title = "品牌管理", businessType = BusinessType.DELETE)
    @Operation(summary = "删除品牌",description = "根据品牌id删除品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(
            @Parameter(name = "ids", description = "品牌id数组")
            @PathVariable Long[] ids)
    {
        return toAjax(brandService.removeBatchByIds(List.of(ids)));
    }

    /**
     * 修改品牌
     */
    @RequiresPermissions("product:brand:edit")
    @Log(title = "品牌管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @Operation(summary = "修改品牌")
    public AjaxResult edit(@Validated @RequestBody Brand brand)
    {
        if (!brandService.checkBrandNameUnique(brand))
        {
            return error("修改品牌'" + brand.getName() + "'失败，品牌名称已存在");
        }
        brand.setUpdateBy(SecurityUtils.getUsername());//设置修改人
        return toAjax(brandService.updateById(brand));
    }

    /**
     * 新增品牌
     */
    @RequiresPermissions("product:brand:add")
    @Log(title = "品牌管理", businessType = BusinessType.INSERT)
    @PostMapping
    @Operation(summary = "新增品牌")
    public AjaxResult add(@Validated @RequestBody Brand brand)
    {
        if (!brandService.checkBrandNameUnique(brand))
        {
            return error("新增品牌'" + brand.getName() + "'失败，品牌名称已存在");
        }
        brand.setCreateBy(SecurityUtils.getUsername());//设置新增人
        return toAjax(brandService.save(brand));
    }

    /**
     * 获取品牌详情
     */
    @Operation(summary = "获取品牌详细信息")
    @RequiresPermissions("product:brand:query")
    @GetMapping(value = "/{brandId}")
    public AjaxResult getInfo(@PathVariable Long brandId)
    {
        return success(brandService.getById(brandId));
    }

    /**
     * 获取品牌列表
     */
    @Operation(summary = "获取品牌列表")
    @GetMapping("/getBrandAll")
    public AjaxResult getBrandAll()
    {
        List<Brand> list = brandService.list();
        return success(list);
    }

}
